<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;


/**
* 商品分类控制器
*/
class ClassController extends Controller
{
    /**
     * 查询并显示所有类型
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        //查询所有类型
        $data = DB::table('goods_class')
                     ->select(DB::raw("concat(path,',',id) as paths,title,id,status"))
                     ->orderBy('paths')
                     ->get();
        
        //格式化字端title
        foreach ($data as $k => $v) {

            $c = substr_count($data[$k]['paths'], ',');
            
            $str = str_repeat('|---', $c);
            $data[$k]['title'] = $str.$data[$k]['title'];
        }

        //显示添加列表
        return view('admin/class/index',['data' => $data]);
    }

    /**
     * 显示添加类别 可具体显示 界面
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd($id = '')
    {
        //查询数据库
        $data = DB::table('goods_class')
                     ->select(DB::raw("concat(path,',',id) as paths,title,id"))
                     ->orderBy('paths')
                     ->get();
        
        //格式化字段title
        foreach ($data as $k => $v) {

            $c = substr_count($data[$k]['paths'], ',');
            
            $str = str_repeat('|---', $c);
            $data[$k]['title'] = $str.$data[$k]['title'];
        }
       
        //显示添加列表
        return view('admin/class/add',['data' => $data,'pid' => $id]);
    }

    /**
     * 处理增加类别
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postInsert(Request $request)
    {
        //接收数据
        $data = $request -> only('pid','title');

        //检测数据
        dd($data);
        //补全path
        if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            $a = DB::table('goods_class') -> where('id',$data['pid']) -> select('path') -> first();
            $data['path'] = $a['path'].','.$data['pid'];
        }

        //插入数据
        $res = DB::table('goods_class') -> insert($data);
       
        //返回结果
        return self::res($res,'添加','/admin/class/index','/admin/class/add');

    }

    /**
     * 编辑类别信息
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit($id)
    {
        //查询当前类别信息
        $first = DB::table('goods_class') -> where('id',$id) -> first();
        $title = $first['title'];
        $pid = $first['pid'];
        
        //查询当前所有分类
        $data = DB::table('goods_class')
                     ->select(DB::raw("concat(path,',',id) as paths,title,id"))
                     ->orderBy('paths')
                     ->get();
        
        //格式化名称字段
        foreach ($data as $k => $v) {

            $c = substr_count($data[$k]['paths'], ',');
            
            $str = str_repeat('|---', $c);
            $data[$k]['title'] = $str.$data[$k]['title'];
        }
       
        //显示添加列表
        return view('admin/class/edit',['data' => $data,'pid' => $pid,'title' => $title,'id' => $id]);
    }

    /**
     * 修改类别信息
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request, $id)
    {
        //接收数据
        $data = $request -> only('pid','title');

        //检测数据

        //补全path
        if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            $a = DB::table('goods_class') -> where('id',$data['pid']) -> select('path') -> first();
            $data['path'] = $a['path'].','.$data['pid'];
        }

        //插入数据
        $res = DB::table('goods_class') -> where('id',$id) -> update($data);
       
        //返回结果
        return self::res($res,'修改','/admin/class/index','/admin/class/edit/'.$id);
    }


    /**
    * 处理结果函数
    */
     static public function res($res,$message,$url_su,$url_er){
        if ($res) {
         
           //跳转到员工列表并显示成功信息
           return redirect($url_su)
                        ->with(['message'=>$message.'成功','type'=>1]);
        }else{

           //跳转到添加页面
           return redirect($url_er)
                        ->with(['message'=>$message.'失败','type'=>2]);
        }
    }
}
